package com.property.manage.controller.owner;

import com.guardchina.framework.mybatis.model.CustomPage;
import com.guardchina.framework.util.exception.BusinessException;
import com.property.manage.model.owner.OwnerCountVo;
import com.property.manage.model.owner.OwnerEntity;
import com.property.manage.model.owner.OwnerVo;
import com.property.manage.service.owner.OwnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * 描述
 *
 * @author lwb
 * @since 2020/7/15
 */
@Api(tags = "业主管理")
@RestController
@RequestMapping("/owner")
@AllArgsConstructor
@Validated
public class OwnerController {

    private OwnerService ownerService;


    @ApiOperation("业主列表")
    @GetMapping("/page")
    public CustomPage<OwnerVo> userPage(@ApiParam("当前页码") @RequestParam("pageNo") @NotNull(message = "请输入当前页码") Long pageNo,
                                        @ApiParam("每页条数") @RequestParam("pageSize") @NotNull(message = "请输入每页条数") Long pageSize,
                                        @ApiParam("业主名称") @RequestParam(value = "ownerName", required = false) String ownerName,
                                        @ApiParam("手机号码") @RequestParam(value = "phone", required = false) String phone,
                                        @ApiParam("小区id") @RequestParam(value = "quarterId", required = false) Integer quarterId,
                                        @ApiParam("业主类型") @RequestParam(value = "type", required = false) Integer type,
                                        @ApiParam("楼栋") @RequestParam(value = "building", required = false) Integer building,
                                        @ApiParam("单元") @RequestParam(value = "unit", required = false) Integer unit,
                                        @ApiParam("房号") @RequestParam(value = "roomNumber", required = false) String roomNumber){
        return ownerService.ownerPage(pageNo, pageSize, ownerName,phone,quarterId,type,building,unit,roomNumber);
    }


    @ApiOperation("添加")
    @PostMapping("/add")
    public void add(@ApiParam("业主类型 1业主 2租户")@RequestParam(value = "type")@NotNull(message = "类型不能为空") Integer type,
                    @ApiParam("名称")@RequestParam(value = "ownerName")@NotBlank(message = "业主名称不能为空") String ownerName,
                    @RequestParam(value = "phone")@NotBlank(message = "手机号码不能为空") String phone,
                    @ApiParam("小区id")@RequestParam(value = "buildingId") @NotNull(message = "楼栋id不能为空")Integer buildingId

    ){
        ownerService.add(ownerName,type,phone,buildingId);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public void update(@RequestParam(value = "id")@NotNull(message = "id不能为空") Integer id,@RequestParam(value = "type")@NotNull(message = "类型不能为空") Integer type,@RequestParam(value = "ownerName")@NotBlank(message = "业主名称不能为空") String ownerName,
                       @RequestParam(value = "phone")@NotBlank(message = "手机号码不能为空") String phone,@RequestParam(value = "buildingId") @NotNull(message = "楼栋id不能为空")Integer buildingId
    ){
       ownerService.update(id,ownerName,type,phone,buildingId);
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    public void delete(@ApiParam("业主id") @RequestParam("id") @NotNull(message = "id为空") Integer id){
        ownerService.delete(id);
    }
    @ApiOperation("统计小区住户信息")
    @GetMapping("/findCount")
    public OwnerCountVo findCount(@ApiParam("小区id") @RequestParam(value = "quarterId",required = false)  Integer quarterId){
        return ownerService.findCount(quarterId);
    }

    @ResponseBody
    @GetMapping("/downOwner")
    @ApiOperation(value = "下载模板", responseContainer = "List", notes = "下载")
    public void downOwner(HttpServletRequest request, HttpServletResponse response) {
        ownerService.downOwner(request,response);
    }

    /**
     * 上传Excel接口
     */
    @PostMapping( "/uploading")
    @ResponseBody
    @ApiOperation(value = "项目新增上传Excel接口")
    //@LocalLock(key = "localLock:test:arg[0]")
    public void uploading(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "quarterId")Integer quarterId) throws InvocationTargetException, IllegalAccessException, IOException {
        String name = file.getOriginalFilename();
        if (!name.endsWith(".xlsx")) {
            throw new BusinessException("文件类型错误！",String.format("文件类型错误"));
        } else {
            ownerService.uploading(file,quarterId);
        }
    }

    @ApiOperation("根据楼栋编号查询一个业主信息")
    @GetMapping("/find/one")
    public OwnerEntity findOne(@ApiParam("楼栋编号") @RequestParam(value = "buildingId") Integer buildingId){
        return ownerService.findOneByBuildingId(buildingId);
    }
}
